<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>the nbro&apos;s blog</title>
    <description>A blog dedicated to Computer Science, Artificial Intelligence and Software Engineering.

Do not copy or use anything from this blog without my permission. If you want to refer to anything that I&apos;ve written here, you can cite this blog.</description>
    <link>https://nbro.gitlab.io//</link>
    <atom:link href="https://nbro.gitlab.io//feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Optionals in Java</title>
        <description>&lt;p&gt;In Java, &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;optionals&lt;/code&gt;&lt;/a&gt; (introduced in Java 1.8) are not strictly needed. In fact, you can write code that uses optionals that is as verbose or even more verbose than without them.&lt;/p&gt;

&lt;p&gt;Without optionals&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Just do a usual null check &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// before calling a method or property on the object&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With optionals&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ofNullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// This is similar to a null check&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isPresent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ofNullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my view, optionals were not optimally implemented in Java because of this and because we have both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;of&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ofNullable&lt;/code&gt; to create optionals. I don’t see why we need &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;of&lt;/code&gt;, which raises an exception if the object is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;. Clearly, if we want to use optionals, we know that they may be null (or empty), so we would just need &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ofNullable&lt;/code&gt;, which doesn’t raise an exception if the argument is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;So, given these &lt;strong&gt;bad design decisions&lt;/strong&gt;, why would people even use optionals?&lt;/p&gt;

&lt;p&gt;I see only 2 main advantages of optionals&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;We can use the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orElse&lt;/code&gt; to get default values (instead of using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isPresent&lt;/code&gt; combined with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get&lt;/code&gt;). The example above would be equivalent&lt;/p&gt;

    &lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ofNullable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
 &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;orElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;They encourage you to think of the absence of values.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;However, null pointer exceptions were already infamous in Java and people should already be forced to do null checks.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are other methods in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt; that people may occasionally find useful, but in many cases maybe not. So, I think it’s more a matter of taste or convention whether you use optionals or not. In other words, Java optionals may be optional &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. I may change opinion the more I use Java and optionals, so I may update this post accordingly.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;In Rust, I like &lt;a href=&quot;https://doc.rust-lang.org/std/option/&quot;&gt;optionals&lt;/a&gt; because Rust programs are written with optionals everywhere and they seem natural. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Mon, 29 Apr 2024 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2024/04/29/optionals-in-java/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2024/04/29/optionals-in-java/</guid>
      </item>
    
      <item>
        <title>MDPs are Markov Games</title>
        <description>&lt;h2 id=&quot;mdps&quot;&gt;MDPs&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Markov Decision Process (MDP)&lt;/strong&gt; is the mathematical framework that can be used to model (sequential) stochastic decision-making problems, which are solved by a &lt;em&gt;single&lt;/em&gt; (Reinforcement Learning) agent. Formally, an MDP can be defined as a tuple&lt;/p&gt;

\[\text{MDP} = (\mathcal{S}, \mathcal{A}, T, r, \gamma),\]

&lt;p&gt;where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(\mathcal{S}\) is the state space&lt;/li&gt;
  &lt;li&gt;\(\mathcal{A}\) is the action space&lt;/li&gt;
  &lt;li&gt;\(T = p(s&apos; \mid s, a)\) is the transition function&lt;/li&gt;
  &lt;li&gt;\(r(s, a)\) is the reward function&lt;/li&gt;
  &lt;li&gt;\(\gamma\) is the discount factor&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;markov-games&quot;&gt;Markov Games&lt;/h2&gt;

&lt;p&gt;A &lt;a href=&quot;https://courses.cs.duke.edu/spring07/cps296.3/littman94markov.pdf&quot;&gt;&lt;strong&gt;Markov Game (MG)&lt;/strong&gt;&lt;/a&gt; (also called &lt;a href=&quot;https://www.ncbi.nlm.nih.gov/pmc/articles/PMC1063912/&quot;&gt;&lt;strong&gt;Stochastic Game&lt;/strong&gt;&lt;/a&gt;) is a mathematical formalism to describe problems where we have &lt;em&gt;multiple&lt;/em&gt; agents (or players) that interact (cooperate, compete or both) with each other, which is the type of problem studied in Game Theory. Formally, an MG with \(n\) agents can be defined as a tuple&lt;/p&gt;

\[\text{MG} = (\mathcal{S}, \mathbb{A}, \Gamma, \mathbb{r}, \gamma),\]

&lt;p&gt;where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(\mathcal{S}\) is the state space&lt;/li&gt;
  &lt;li&gt;\(\mathbb{A} = \{\mathcal{A}_1, \dots, \mathcal{A}_n\}\) is a set of action spaces, one for each of the \(n\) agents&lt;/li&gt;
  &lt;li&gt;\(\Gamma = p(s&apos; \mid s, a_1, \dots, a_n)\) is the transition function&lt;/li&gt;
  &lt;li&gt;\(\mathbb{r} = \{ r_1(s, a_1, \dots, a_n), \dots, r_n(s, a_1, \dots, a_n) \}\) is a set of reward functions, one for each agent&lt;/li&gt;
  &lt;li&gt;\(\gamma\) is the discount factor&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;mdps-are-markov-games&quot;&gt;MDPs are Markov Games&lt;/h3&gt;

&lt;p&gt;So, what are the differences between an MDP and an MG?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;In an MG, we have a set of action spaces, one for each agent, while in an MDP we have only one action space,&lt;/li&gt;
  &lt;li&gt;In an MG, the transition function conditions on the action of all players, and&lt;/li&gt;
  &lt;li&gt;In an MG, we have a set of reward functions, one for each agent, where each reward function also conditions on the action of all players.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An MDP can therefore be viewed as a Markov Game with one player.&lt;/p&gt;

</description>
        <pubDate>Sun, 17 Mar 2024 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2024/03/17/mdps-are-markov-games/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2024/03/17/mdps-are-markov-games/</guid>
      </item>
    
      <item>
        <title>Bellman Equations</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;In &lt;a href=&quot;http://incompleteideas.net/book/RLbook2020.pdf&quot;&gt;&lt;em&gt;Reinforcement Learning (RL)&lt;/em&gt;&lt;/a&gt;, &lt;strong&gt;value functions&lt;/strong&gt; define the objectives of the RL problem. There are two very important and strictly related value functions,&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the &lt;strong&gt;state-action value function (SAVF)&lt;/strong&gt; &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, and&lt;/li&gt;
  &lt;li&gt;the &lt;strong&gt;state value function (SVF)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this post, I’ll show how these value functions (including their &lt;em&gt;optimality&lt;/em&gt; versions) can be mathematically formulated as recursive equations, known as &lt;strong&gt;Bellman equations&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I’ll assume that you are minimally familiar with &lt;strong&gt;Markov Decision Processes (MDPs)&lt;/strong&gt; and RL. Nevertheless, I will review the most important RL and mathematical prerequisites to understand this post, so that the post is self-contained as much as possible.&lt;/p&gt;

&lt;!-- (without making this very very long). For more details, I recommend that you read the related sections of the book [Reinforcement Learning: An Introduction][1] (2nd edition) by Sutton and Barto. Needless to say, you can skip the sections on the topics you already know well. --&gt;

&lt;h2 id=&quot;notation&quot;&gt;Notation&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Stylized upper case letters (e.g. \(\mathcal{X}\) or \(\mathbb{R}\)) denote &lt;em&gt;vector spaces&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;Upper case letters (e.g. \(X\)) denote &lt;a href=&quot;https://en.wikipedia.org/wiki/Random_variable&quot;&gt;&lt;em&gt;random variables&lt;/em&gt;&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Depending on the context, lower case letters (e.g. \(x\)) can denote &lt;a href=&quot;https://www.statlect.com/glossary/realization-of-a-random-variable&quot;&gt;&lt;em&gt;realizations&lt;/em&gt;&lt;/a&gt; of random variables, &lt;em&gt;variables&lt;/em&gt; of functions, or &lt;em&gt;elements&lt;/em&gt; of a vector space.&lt;/li&gt;
  &lt;li&gt;Depending on the context, \(\color{blue}{p}(s&apos; \mid s, a)\) can be a shorthand for \(\color{blue}{p}(S&apos;=s&apos; \mid S=s, A=a) \in [0, 1]\), a probability, or \(\color{blue}{p}(s&apos; \mid S=s, A=a)\), a conditional probability distribution.&lt;/li&gt;
  &lt;li&gt;\(X=x\) is an &lt;em&gt;event&lt;/em&gt;, which is occasionally abbreviated as \(x\).&lt;/li&gt;
  &lt;li&gt;\(s&apos; \sim \color{blue}{p}(s&apos; \mid s, a)\) means that \(s&apos;\) is drawn/sampled according to \(\color{blue}{p}\).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;markov-decision-processes&quot;&gt;Markov Decision Processes&lt;/h2&gt;

&lt;p&gt;A (discounted) &lt;a href=&quot;https://www.gwern.net/docs/statistics/decision/1960-howard-dynamicprogrammingmarkovprocesses.pdf&quot;&gt;&lt;strong&gt;MDP&lt;/strong&gt;&lt;/a&gt; can be defined as a tuple&lt;/p&gt;

\[M 
\triangleq 
(\mathcal{S}, \mathcal{A}, \mathcal{R}, \color{blue}{p}, \mathscr{r}, \gamma) 
\tag{1} \label{1},\]

&lt;ul&gt;
  &lt;li&gt;\(\mathcal{S}\) is the &lt;em&gt;state space&lt;/em&gt;,&lt;/li&gt;
  &lt;li&gt;\(\mathcal{A}\) is the &lt;em&gt;action space&lt;/em&gt;,&lt;/li&gt;
  &lt;li&gt;\(\mathcal{R} \subset \mathbb{R}\) is the &lt;em&gt;reward space&lt;/em&gt;,&lt;/li&gt;
  &lt;li&gt;\(\color{blue}{p}(s&apos; \mid s, a)\) is the &lt;em&gt;transition model&lt;/em&gt;,&lt;/li&gt;
  &lt;li&gt;\(\mathscr{r}(s, a) = \mathbb{E}_{p(r \mid s, a)} \left[ R \mid S = s, A = a \right]\) is the &lt;em&gt;expected reward&lt;/em&gt;, and&lt;/li&gt;
  &lt;li&gt;\(\gamma \in [0, 1]\) is the &lt;em&gt;discount factor&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;markov-property&quot;&gt;Markov Property&lt;/h3&gt;

&lt;p&gt;MDPs assume that the &lt;strong&gt;Markov property&lt;/strong&gt; holds, i.e. the future is independent of the past given the present. The Markov property is encoded in \(\color{blue}{p}(s&apos; \mid s, a)\) and \(\mathscr{r}(s, a)\).&lt;/p&gt;

&lt;h3 id=&quot;finite-mdps&quot;&gt;Finite MDPs&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;finite MDP&lt;/strong&gt; is an MDP where the state \(\mathcal{S}\), action \(\mathcal{A}\) and rewad \(\mathcal{R}\) spaces are &lt;a href=&quot;https://en.wikipedia.org/wiki/Finite_set&quot;&gt;finite sets&lt;/a&gt;. In that case,  \(\color{blue}{p}(s&apos; \mid s, a)\) can be viewed as a &lt;a href=&quot;https://en.wikipedia.org/wiki/Probability_mass_function&quot;&gt;&lt;em&gt;probability mass function (pmf)&lt;/em&gt;&lt;/a&gt; and the random variable associated with states, actions and rewards are &lt;em&gt;discrete&lt;/em&gt;.&lt;/p&gt;

&lt;h3 id=&quot;alternative-formulations&quot;&gt;Alternative Formulations&lt;/h3&gt;

&lt;p&gt;Sometimes, \(\color{blue}{p}(s&apos; \mid s, a)\) is combined with \(\mathscr{r}(s, a)\) to form a joint conditional distribution, \(\color{purple}{p}(s&apos;, r \mid s, a)\) (the &lt;em&gt;dynamics&lt;/em&gt; of the MDP), from which both \(\color{blue}{p}(s&apos; \mid s, a)\) and \(\mathscr{r}(s, a)\) can be derived.&lt;/p&gt;

&lt;p&gt;Specifically, \(\color{blue}{p}(s&apos; \mid s, a)\) can be computed from \(\color{purple}{p}(s&apos;, r \mid s, a)\) by marginalizing over \(r\) &lt;sup id=&quot;fnref:11&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:11&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; as follows&lt;/p&gt;

\[\color{blue}{p}(s&apos; \mid s, a) = \sum_{r \in \mathcal{R}}r \color{purple}{p}(s&apos;, r \mid s, a).
\tag{2} \label{2}\]

&lt;p&gt;Similarly, we have&lt;/p&gt;

\[\begin{align*}
\mathscr{r}(s, a) 
&amp;amp;= 
\mathbb{E} \left[ R \mid S = s, A = a \right] 
\\
&amp;amp;= \sum_{r \in \mathcal{R}} r \sum_{s&apos; \in \mathcal{S}} \color{purple}{p}(s&apos;, r \mid s, a) 
\\ 
&amp;amp;= 
\sum_{r \in \mathcal{R}} r p(r \mid s, a).
\end{align*}
\tag{3} \label{3}\]

&lt;!-- You can also define the reward function as a function of the next state $$s&apos;$$ too, i.e. 

$$
\begin{align*}
\mathscr{r}(s, a, s&apos;) 
&amp;= 
\mathbb{E} \left[ R \mid S = s, A = a, S&apos; = s&apos; \right] 
\\
&amp;= 
\sum_{r \in \mathcal{R}} r \frac{\color{purple}{p}(s&apos;, r \mid s, a)}{\color{blue}{p}(s&apos; \mid s, a)} 
\\
&amp;= 
\sum_{r \in \mathcal{R}} r p(r \mid s&apos;, s, a).
\end{align*}
$$ --&gt;

&lt;!-- ### Illustration

Here&apos;s a diagram (taken from Wikipedia) of an MDP $$M$$, where $$\mathcal{S} = \{ s_0, s_1, s_2 \}$$ (green circles), $$\mathcal{A} = \{ a_0, a_1 \}$$ (orange circles), $$\mathcal{R} = \{ -1, 5, 0 \}$$ (orange arrows), the black arrows represent the transitions (with the corresponding probabilities),the orange arrows are the rewards (the rewards of zero are not shown).

![An example of an MDP](/images/mdp.png)

To give you an idea of the transition probabilities, the probability of transition from $$s_0$$ to $$s_0$$ by taking action $$a_0$$ is $$\color{blue}{p}(S&apos;=s_0 \mid S = s_0, A=a_0) = 0.5$$, while the probability of transitioning from $$s_0$$ to $$s_2$$ by taking action $$a_1$$ is $$\color{blue}{p}(S&apos;=s_2 \mid S = s_0, A=a_1) = 1.0$$. The expected reward of taking the action $$a_0$$ in $$s_1$$ and ending up in $$s_2$$ is $$\mathscr{r}(s_1, a_0, s_2) = 0$$, while $$\mathscr{r}(s_1, a_0, s_0) = 5$$. --&gt;

&lt;h2 id=&quot;reinforcement-learning&quot;&gt;Reinforcement Learning&lt;/h2&gt;

&lt;!-- ### Agent-Environment Interaction --&gt;

&lt;p&gt;In &lt;a href=&quot;http://incompleteideas.net/book/RLbook2020.pdf&quot;&gt;&lt;strong&gt;RL&lt;/strong&gt;&lt;/a&gt;, we imagine that there is an &lt;strong&gt;agent&lt;/strong&gt; that &lt;em&gt;sequentially&lt;/em&gt; &lt;em&gt;interacts&lt;/em&gt; with an &lt;strong&gt;environment&lt;/strong&gt; in (discrete) &lt;em&gt;time-steps&lt;/em&gt;, where the environment can be modelled as an MDP.&lt;/p&gt;

&lt;!-- So, RL is used to solve sequential decision-making problems (hence the emphasis on _sequentially_!), which can be modelled as MDPs. --&gt;

&lt;p&gt;More specifically, at time-step \(t\), the agent is in some &lt;strong&gt;state&lt;/strong&gt; \(s_t \in \mathcal{S}\) &lt;sup id=&quot;fnref:8&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:8&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; and takes an &lt;strong&gt;action&lt;/strong&gt; \(a_t \in \mathcal{A}\) with a &lt;strong&gt;policy&lt;/strong&gt; \(\color{red}{\pi}(a \mid s)\), which is a conditional probability distribution over actions given a state, i.e. \(a_t \sim \pi(a \mid s_t)\). At the next time step \(t+1\), the environment returns a &lt;strong&gt;reward&lt;/strong&gt; \(r_{t+1} = \mathscr{r}(s_t, a_t)\), and it moves to another state \(s_{t+1} \sim \color{blue}{p}(s&apos; \mid s_t, a_t)\), then the agent takes another action \(a_{t+1} \sim \color{red}{\pi}(a \mid s_{t+1})\), gets another reward \(r_{t+2} = \mathscr{r}(s_{t+1}, a_{t+1})\), and the environment moves to another state \(s_{t+2}\), and so on. This interaction continues until a maximum time-step \(H\), which is often called &lt;strong&gt;horizon&lt;/strong&gt;, is reached. For simplicity, we assume that \(H = \infty\), so we assume a so-called &lt;em&gt;infinite-horizon MDP&lt;/em&gt;.&lt;/p&gt;

&lt;!-- Here&apos;s a diagram that illustrates this interaction (taken from Sutton &amp; Barto&apos;s book), where the random variables $$A_t, S_{t+1}, R_{t+1}, S_t$$ and $$R_t$$ (rather their [realizations][4] $$a_t, s_{t+1}, r_{t+1}, s_t$$ and $$r_t$$) are used to emphasize that this interaction is stochastic.

![A diagram of the Agent-Environment Interaction](/images/agent-environment-interaction.png) --&gt;

&lt;!-- ### Finite and Infinite-Horizon MDPs

If $$H$$ is finite, then we have _finite-horizon MDP_, which can be used to describe tasks that can naturally be broken into _episodes_ (e.g. a sequence of games of chess, where each game is an episode), i.e. _episodic tasks_. If $$H = \infty$$, then we have an _infinite-horizon MDP_, which can be used to describe _continuing tasks_. In the case of episodic tasks, $$H$$ is a random variable, given that the time-step the episode terminates might not always be the same.  --&gt;

&lt;!-- ### Objective --&gt;

&lt;p&gt;In RL, the objective/goal is to find a policy that &lt;em&gt;maximizes&lt;/em&gt; the &lt;em&gt;sum of rewards in the long run&lt;/em&gt;, i.e. until the horizon \(H\) is reached (if ever reached). An objective function that formalizes this sum of rewards in the long run is the &lt;strong&gt;state-action value function (SAVF)&lt;/strong&gt;, which is, therefore, one function that we might want to optimize.&lt;/p&gt;

&lt;!-- ### RL Algorithms

The most known RL algorithms are probably [Q-learning][13] and [SARSA][14]. They implement the agent-environment interaction described above in some specific way, in order to estimate the SAVF and then derive the policy from it. There are also algorithms that estimate the policy directly (e.g. [REINFORCE][15]). --&gt;

&lt;h2 id=&quot;state-action-value-function&quot;&gt;State-Action Value Function&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;state-action value function&lt;/em&gt; for a policy \(\color{red}{\pi}(a \mid s)\) is the function \(q_\color{red}{\pi} : \mathcal{S} \times \mathcal{A} \rightarrow \mathbb{R}\), which is defined as follows&lt;/p&gt;

\[q_\color{red}{\pi}(s, a)
\triangleq
\mathbb{E} \left[ \sum_{k=0}^\infty \gamma^k R_{t+k+1} \mid S_t = s, A_t = a\right],
\\ \color{orange}{\forall} s \in \mathcal{S}, \color{orange}{\forall} a \in \mathcal{A}
\tag{4}\label{4},\]

&lt;p&gt;where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(R_{t+k+1}\) is the &lt;em&gt;reward&lt;/em&gt; the agent receives at time-step \(t+k+1\),&lt;/li&gt;
  &lt;li&gt;\(G_t \triangleq \sum_{k=0}^\infty \gamma^k R_{t+k+1}\) is the &lt;em&gt;return&lt;/em&gt; (aka &lt;em&gt;value&lt;/em&gt; &lt;sup id=&quot;fnref:4&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:4&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;),&lt;/li&gt;
  &lt;li&gt;\(\color{red}{\pi}\) is a &lt;em&gt;policy&lt;/em&gt; that the agents follows from time-step \(t+1\) onwards,&lt;/li&gt;
  &lt;li&gt;\(\gamma \in [0, 1)\) is the &lt;em&gt;discount factor&lt;/em&gt;,&lt;/li&gt;
  &lt;li&gt;\(S_t = s\) is the &lt;em&gt;state&lt;/em&gt; the agent is in at time-step \(t\), and&lt;/li&gt;
  &lt;li&gt;\(A_t = a\) is the &lt;em&gt;action&lt;/em&gt; taken at time-step \(t\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Intuitively, \(q_\color{red}{\pi}(s, a)\) is &lt;strong&gt;expected return&lt;/strong&gt; that the agent gets by following policy \(\color{red}{\pi}\) &lt;strong&gt;after&lt;/strong&gt; having taken action \(a\) in state \(s\) at time-step \(t\).&lt;/p&gt;

&lt;!-- ### Discount Factor

Here, $$\gamma$$ is used to unify the objective of episodic and continuing tasks and to weight the importance of the rewards with respect to when they are received.  --&gt;

&lt;!-- In case $$H = \infty$$, we need $$\gamma \in [0, 1)$$ and the sequence $$\{ R_{t+k+1} \}$$ to be bounded, in order to make the sum finite, which is desirable because we want to maximise the SAVF. Specifically, if the maximum possible reward is $$R_\text{max}$$, then the return is bounded by $$\frac{R_\text{max}}{1 - \gamma}$$, i.e.

$$
\begin{align*}
G_t 
&amp;\triangleq 
\sum_{k=0}^\infty \gamma^k R_{t+k+1} \leq \sum_{k=0}^\infty \gamma^k R_\text{max} 
\\
&amp;= 
\frac{R_\text{max}}{1 - \gamma},
\end{align*}
$$ 

where $$\sum_{k=0}^\infty \gamma^k R_\text{max}$$ is a [geometric series][16]. 

In case $$H &lt; \infty$$, the return is finite, even if we use $$\gamma = 1$$.  --&gt;

&lt;!-- ### Unified View

If $$H = \infty$$, equation \ref{1} can still represent the objective function of an episodic problem, but we need to add a special state to the state space, called the _absorbing state_, in which 

- the agent remains forever after the episode terminates, 
- all actions have no effect (i.e. it remains in the absorbing state), and 
- the agents only gets a reward of $$0$$. --&gt;

&lt;h3 id=&quot;value-function-of-a-policy&quot;&gt;Value Function of a Policy&lt;/h3&gt;

&lt;p&gt;The subscript \(\color{red}{\pi}\) in \(q_\color{red}{\pi}(s, a)\) indicates that \(q_\color{red}{\pi}(s, a)\) is defined in terms of \(\color{red}{\pi}(a \mid s)\) because the rewards received in the future, \(R_{t+k+1}\), depend on the actions that we take with \(\color{red}{\pi}(s \mid a)\), but they also depend on the &lt;em&gt;transition model&lt;/em&gt; \(\color{blue}{p}(s&apos; \mid s, a)\).&lt;/p&gt;

&lt;p&gt;However, \(\color{red}{\pi}\) and \(\color{blue}{p}\) do not appear anywhere inside the expectation in equation \ref{4}. So, for people that only believe in equations, \ref{4} might not be satisfying enough. Luckily, we can express \(q_\color{red}{\pi}(s, a)\) in terms of \(\color{red}{\pi}\) and \(\color{blue}{p}\) by starting from equation \ref{4}, which also leads to a Bellman/recursive equation. So, let’s do it!&lt;/p&gt;

&lt;h2 id=&quot;mathematical-prerequisites&quot;&gt;Mathematical Prerequisites&lt;/h2&gt;

&lt;p&gt;The formulation of the value functions as recursive equations (which is the main point of this blog post) uses three main mathematical rules, which are reviewed here for completeness.&lt;/p&gt;

&lt;h3 id=&quot;markov-property-1&quot;&gt;Markov Property&lt;/h3&gt;

&lt;p&gt;If we assume that the &lt;em&gt;Markov property&lt;/em&gt; holds, then the following holds&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
\[\color{blue}{p}(s_{t+1} \mid s_t, a_t, s_{t-1}, a_{t-1}, \dots, s_0, a_0) = \color{blue}{p}(s_{t+1} \mid s_t, a_t)\]
  &lt;/li&gt;
  &lt;li&gt;
\[\mathscr{r}(s, a) = \mathbb{E} \left[ R_{t+1}\mid s_t, a_t, s_{t-1}, a_{t-1}, \dots, s_0, a_0 \right] = \mathbb{E} \left[ R_{t+1}\mid s_t, a_t \right]\]
  &lt;/li&gt;
  &lt;li&gt;
\[\color{red}{\pi}(a_{t} \mid s_t, s_{t-1}, a_{t-1}, \dots, s_0, a_0 ) = \color{red}{\pi}(a_{t} \mid s_t)\]
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;linearity-of-expectation-le&quot;&gt;Linearity of Expectation (LE)&lt;/h3&gt;

&lt;p&gt;Let \(X\) and \(Y\) be two discrete random variables and \(p(x, y)\) be their joint distribution, then the expectation of \(X+Y\) is equal to the sum of the expectation of \(X\) and \(Y\), i.e.&lt;/p&gt;

\[\begin{align*}
\mathbb{E}[X + Y] 
&amp;amp;= \sum_x \sum_y  (x + y) p(x, y)
\\
&amp;amp;= \sum_x \sum_y  x p(x, y) + y p(x, y)
\\
&amp;amp;= \sum_x \sum_y  x p(x, y) + \sum_x \sum_y y p(x, y)
\\
&amp;amp;= \sum_x x p(x) + \sum_y y p(y)
\\
&amp;amp;= \mathbb{E}[X] + \mathbb{E}[Y] 
\end{align*}\]

&lt;h3 id=&quot;law-of-total-expectation-lte&quot;&gt;Law of Total Expectation (LTE)&lt;/h3&gt;

&lt;p&gt;The formulation of the &lt;strong&gt;LTE&lt;/strong&gt; is as follows. Let \(X\), \(Y\) and \(Z\) be three discrete random variables, \(\mathbb{E}[X \mid Y=y]\) be the expectation of \(X\) given \(Y=y\), and \(p(x, y, z)\) be the joint of \(X\), \(Y\) and \(Z\). So, we have&lt;/p&gt;

\[\begin{align*}
\mathbb{E}[X \mid Y=y] 
&amp;amp;= 
\sum_x x p(x \mid y) 
\\
&amp;amp;= 
\sum_x x \frac{p(x, y)}{p(y)} 
\\
&amp;amp;=  
\sum_x x \frac{\sum_z p(x, y, z)}{p(y)} 
\\
&amp;amp;= 
\sum_x x \frac{\sum_z p(x \mid y, z) p(y, z) }{p(y)} 
\\
&amp;amp;= 
\sum_z \frac{p(y, z)}{p(y)} \sum_x x p(x \mid y, z) 
\\
&amp;amp;= 
\sum_z p(z \mid y) \sum_x x p(x \mid y, z) 
\\
&amp;amp;=  
\sum_z p(z \mid y)  \mathbb{E}[X \mid Y=y, Z=z].
\end{align*}\]

&lt;p&gt;This also applies to other more complicated cases, i.e. more conditions, or even to the simpler case of \(\mathbb{E}[X]\).&lt;/p&gt;

&lt;h2 id=&quot;state-action-bellman-equation&quot;&gt;State-Action Bellman Equation&lt;/h2&gt;

&lt;p&gt;We are finally ready to express \(\ref{4}\) as a recursive equation.&lt;/p&gt;

&lt;p&gt;We can decompose the return \(G_t\) into the first reward \(R_{t+1}\), received after having taken action \(A_t = a\) in state \(S_t = s\), and the rewards that we will receive in the next time steps, then we can apply the LE, LTE (multiple times) and Markov property, i.e.&lt;/p&gt;

\[\begin{align*}
q_\color{red}{\pi}(s, a) 
&amp;amp;= 
\mathbb{E} \left[ R_{t+1} + \sum_{k=1}^\infty \gamma^k R_{t+k+2} \mid S_t = s, A_t = a\right] 
\\
&amp;amp;=
\mathbb{E} \left[ R_{t+1} \mid S_t = s, A_t = a \right] + \gamma  \mathbb{E} \left[ G_{t+1} \mid S_t = s, A_t = a\right]
\\
&amp;amp;=
\mathscr{r}(s, a) + \gamma \sum_{s&apos;} \color{blue}{p}(s&apos; \mid s, a)  \mathbb{E} \left[ G_{t+1} \mid S_{t+1}=s&apos;, S_t = s, A_t = a\right] 
\\
&amp;amp;=
\mathscr{r}(s, a) + \gamma \sum_{s&apos;} \color{blue}{p}(s&apos; \mid s, a)  \mathbb{E} \left[ G_{t+1} \mid S_{t+1}=s&apos;\right] 
\\
&amp;amp;=
\mathscr{r}(s, a) + \gamma \sum_{s&apos;} \color{blue}{p}(s&apos; \mid s, a)  v_\color{red}{\pi}(s&apos;) 
\\
&amp;amp;=
\mathscr{r}(s, a) + \gamma \sum_{s&apos;} \color{blue}{p}(s&apos; \mid s, a) \sum_{a&apos;} \color{red}{\pi}(a&apos; \mid s&apos;) \mathbb{E} \left[ G_{t+1} \mid S_{t+1}=s&apos;, A_{t+1}=a&apos;\right]
\\
&amp;amp;=
\mathscr{r}(s, a) + \gamma \sum_{s&apos;} \color{blue}{p}(s&apos; \mid s, a) \sum_{a&apos;} \color{red}{\pi}(a&apos; \mid s&apos;) q_\color{red}{\pi}(s&apos;, a&apos;)
\tag{5}\label{5},
\end{align*}\]

&lt;p&gt;where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(\lambda G_{t+1}=\sum_{k=1}^\infty \gamma^k R_{t+k+2} = \gamma \sum_{k=0}^\infty \gamma^k R_{t+k+2}\),&lt;/li&gt;
  &lt;li&gt;\(\mathscr{r}(s, a) \triangleq \mathbb{E}_{p(r \mid s, a)} \left[ R_{t+1} \mid S_t = s, A_t = a \right] =  \sum_{r} p(r \mid s, a) r\) is the expected reward of taking action \(a\) in \(s\) and \(p(r \mid s, a)\) is the reward distribution &lt;sup id=&quot;fnref:7&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:7&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;,&lt;/li&gt;
  &lt;li&gt;\(v_\color{red}{\pi}(s&apos;) \triangleq \mathbb{E} \left[ G_{t+1} \mid S_{t+1}=s&apos;\right]\) is the &lt;strong&gt;state value function (SVF)&lt;/strong&gt;, and&lt;/li&gt;
  &lt;li&gt;\(q_\color{red}{\pi}(s&apos;, a&apos;) \triangleq \mathbb{E} \left[ G_{t+1} \mid S_{t+1}=s&apos;, A_{t+1}=a&apos;\right]\).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The equation \ref{5} is a &lt;em&gt;recursive equation&lt;/em&gt;, given that \(q_\color{red}{\pi}\) is defined in terms of itself (although evaluated at a different state-action pair), known as the &lt;em&gt;state-action&lt;/em&gt; &lt;strong&gt;Bellman (expectation) equation&lt;/strong&gt; for \(q_\color{red}{\pi}\).&lt;/p&gt;

&lt;p&gt;So, the subscript \(\color{red}{\pi}\) in \(q_\color{red}{\pi}(s, a)\) is used because the state-action value is (also) defined in terms of \(\color{red}{\pi}\).&lt;/p&gt;

&lt;h3 id=&quot;alternative-version&quot;&gt;Alternative Version&lt;/h3&gt;

&lt;p&gt;Given the relations \ref{2} and \ref{3}, equation \ref{5} can also be expressed in terms of \(\color{purple}{p}(s&apos;, r \mid s, a)\) as follows.&lt;/p&gt;

\[\begin{align*}
q_\color{red}{\pi}(s, a) 
&amp;amp;= 
\sum_{r} \underbrace{\sum_{s&apos;} \color{purple}{p}(s&apos;, r \mid s, a)}_{p(r \mid s, a)} r + \gamma \sum_{s&apos;} \underbrace{\sum_{r} \color{purple}{p}(s&apos;, r \mid s, a)}_{\color{blue}{p}(s&apos; \mid s, a)} v_\color{red}{\pi}(s&apos;) 
\\ 
&amp;amp;= 
\sum_{s&apos;} \sum_{r} \color{purple}{p}(s&apos;, r \mid s, a) \left[ r + \gamma  v_\color{red}{\pi}(s&apos;) \right]
\tag{6}\label{6}
.
\end{align*}\]

&lt;p&gt;So, \(q_\color{red}{\pi}(s, a)\) is an expectation with respect to the joint conditional distribution \(\color{purple}{p}(s&apos;, r \mid s, a)\), i.e.&lt;/p&gt;

\[q_\color{red}{\pi}(s, a) 
= 
\mathbb{E}_{\color{purple}{p}(s&apos;, r \mid s, a)} \left[ R_{t+1} + \gamma v_{\color{red}{\pi}}(S_{t+1}) \mid S_t = s, A_t =a\right]
\tag{7}\label{7}
.\]

&lt;p&gt;Equation \ref{6} can also be derived from equation \ref{4} by applying the LTE with respect to \(\color{purple}{p}(s&apos;, r \mid s, a)\).&lt;/p&gt;

&lt;h3 id=&quot;vectorized-form&quot;&gt;Vectorized Form&lt;/h3&gt;

&lt;p&gt;If the MDP is finite, then we can express the state-action Bellman equation in \ref{5} in a vectorized form&lt;/p&gt;

\[\mathbf{Q}_\color{red}{\pi} 
=
\mathbf{R} 
+ 
\gamma 
\mathbf{\color{blue}{P}}
\mathbf{V}_\color{red}{\pi},
\tag{8}\label{8}\]

&lt;p&gt;where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(\mathbf{Q}_\color{red}{\pi} \in \mathbb{R}^{\mid \mathcal{S} \mid \times \mid \mathcal{A} \mid }\) is a matrix that contains the state-action values for each state-action pair \((s, a)\), so \(\mathbf{Q}_\color{red}{\pi}[s, a] = q_{\color{red}{\pi}}(s, a)\),&lt;/li&gt;
  &lt;li&gt;\(\mathbf{R} \in \mathbb{R}^{\mid \mathcal{S} \mid \times \mid \mathcal{A} \mid }\) is a matrix with the expected rewards for each state-action pair \((s, a)\), so \(\mathbf{R}[s, a] = \mathscr{r}(s, a)\),&lt;/li&gt;
  &lt;li&gt;\(\mathbf{\color{blue}{P}} \in \mathbb{R}^{\mid \mathcal{S} \mid \times \mid \mathcal{A} \mid \times \mid \mathcal{S} \mid}\) is a matrix that contains the transition probabilities for each triple \((s, a, s&apos;)\), so \(\mathbf{\color{blue}{P}}[s, a, s&apos;] = \color{blue}{p}(S&apos;=s&apos; \mid S=s, A=a)\), and&lt;/li&gt;
  &lt;li&gt;\(\mathbf{V}_\color{red}{\pi} \in \mathbb{R}^{\mid \mathcal{S} \mid}\) is a vector that contains the state values (as defined in equation \ref{5}) for each state \(s&apos;\), so \(\mathbf{V}_\color{red}{\pi}[s&apos;] = v_\color{red}{\pi}(s&apos;)\).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;optimal-state-action-value-function&quot;&gt;Optimal State-Action Value Function&lt;/h2&gt;

&lt;p&gt;In RL, the goal is to find/estimate an &lt;em&gt;optimal policy&lt;/em&gt;, \(\color{green}{\pi_*}\), i.e. one that, if followed, maximizes the expected return. For a finite MDP, there is a &lt;em&gt;unique optimal state-action value function&lt;/em&gt;, which can be denoted by \(q_{\color{green}{\pi_*}}(s, a)\) or just \(q_\color{green}{*}(s, a)\), from which an optimal policy can be derived.&lt;/p&gt;

&lt;p&gt;By definition, the optimal state-action value function is&lt;/p&gt;

\[q_{\color{green}{\pi_*}}(s, a)
\triangleq
\operatorname{max}_\color{red}{\pi} q_\color{red}{\pi}(s, a), 
\\ 
\color{orange}{\forall} s \in \mathcal{S}, \color{orange}{\forall} a \in \mathcal{A} 
\tag{9}\label{9}
.\]

&lt;p&gt;For a discounted infinite-horizon MDP, the optimal policy is deterministic &lt;sup id=&quot;fnref:6&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:6&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;6&lt;/a&gt;&lt;/sup&gt; and stationary &lt;sup id=&quot;fnref:10&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:10&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;7&lt;/a&gt;&lt;/sup&gt;, and it’s any greedy policy with respect to \(q_{\color{green}{\pi_*}}(s, a)\), i.e.&lt;/p&gt;

\[\color{green}{\pi_*} 
\in 
\operatorname{arg max}_a q_{\color{green}{\pi_*}}(s, a), 
\\ 
\color{orange}{\forall} s \in \mathcal{S} 
\tag{10}\label{10}\]

&lt;p&gt;Here, \(\in\) is used because there can be more than one optimal policy for an MDP given that there can be two or more actions that are optimal in a state.&lt;/p&gt;

&lt;h2 id=&quot;state-action-bellman-optimality-equation&quot;&gt;State-Action Bellman Optimality Equation&lt;/h2&gt;

&lt;p&gt;Equation \ref{9} can also be written as a recursive equation, known as the &lt;em&gt;state-action&lt;/em&gt; &lt;strong&gt;Bellman optimality equation&lt;/strong&gt;.&lt;/p&gt;

\[\begin{align*}
q_{\color{green}{\pi_*}}(s, a)
&amp;amp;\triangleq
\operatorname{max}_\color{red}{\pi} 
q_\color{red}{\pi}(s, a)
\\ 
&amp;amp;=
\operatorname{max}_\color{red}{\pi} 
\sum_{s&apos;} \sum_{r} \color{purple}{p}(s&apos;, r \mid s, a) \left[ r + \gamma  v_\color{red}{\pi}(s&apos;) \right]
\\
&amp;amp;=
\sum_{s&apos;} \sum_{r} \color{purple}{p}(s&apos;, r \mid s, a) \left[ r + \gamma  \operatorname{max}_\color{red}{\pi}  v_\color{red}{\pi}(s&apos;) \right]
\\
&amp;amp;=
\sum_{s&apos;} \sum_{r} \color{purple}{p}(s&apos;, r \mid s, a) \left[ r + \gamma \operatorname{max}_{a&apos;}q_{\color{green}{\pi_*}}(s&apos;, a&apos;) \right]
\\
&amp;amp;=
\mathbb{E}_{\color{purple}{p}(s&apos;, r \mid s, a)} \left[ R_{t+1} + \gamma v_{\color{green}{\pi_*}}(S_{t+1}) \mid S_t = s, A_t =a\right]
\tag{11}\label{11},
\end{align*}\]

&lt;p&gt;where \(v_\color{green}{\pi_*}(s&apos;) = \operatorname{max}_\color{red}{\pi}  v_\color{red}{\pi}(s&apos;) = \operatorname{max}_{a&apos;}q_{\color{green}{\pi_*}}(s&apos;, a&apos;)\).&lt;/p&gt;

&lt;h2 id=&quot;state-bellman-equation&quot;&gt;State Bellman Equation&lt;/h2&gt;

&lt;p&gt;Like \(q_\color{red}{\pi}(s, a)\), the state value function \(v_\color{red}{\pi}(s)\) can also be written as a recursive equation by starting from its definition and then applying the LTE rule, the linearity of the expectation and the Markov property. So, for completeness, let’s do it.&lt;/p&gt;

\[\begin{align*}
v_\color{red}{\pi}(s) 
&amp;amp;\triangleq 
\mathbb{E} \left[ G_t \mid S_t=s\right] 
\\
&amp;amp;=
\mathbb{E} \left[ R_{t+1} + \gamma G_{t+1}  \mid S_t=s \right] 
\\
&amp;amp;=
\mathbb{E} \left[ R_{t+1} \mid S_t=s \right]  + \gamma \mathbb{E} \left[  G_{t+1}  \mid S_t=s \right] 
\\
&amp;amp;=
 \sum_{a} \color{red}{\pi}(a \mid s) \mathscr{r}(s, a) + \gamma \sum_{a} \color{red}{\pi}(a \mid s)  \mathbb{E} \left[ G_{t+1} \mid S_t = s, A_t = a\right]
 \\
&amp;amp;=
 \sum_{a} \color{red}{\pi}(a \mid s) \mathscr{r}(s, a) + \gamma \sum_{a} \color{red}{\pi}(a \mid s) \sum_{s&apos;} \color{red}{p}(s&apos; \mid s, a)  \mathbb{E} \left[ G_{t+1} \mid S_{t+1} = s&apos;\right]
  \\
&amp;amp;=
 \sum_{a} \color{red}{\pi}(a \mid s) \mathscr{r}(s, a) + \gamma \sum_{a} \color{red}{\pi}(a \mid s) \sum_{s&apos;} \color{red}{p}(s&apos; \mid s, a)  v_\color{red}{\pi}(s&apos;) 
   \\
&amp;amp;=
 \sum_{a} \color{red}{\pi}(a \mid s) \left ( \mathscr{r}(s, a) + \gamma  \sum_{s&apos;} \color{red}{p}(s&apos; \mid s, a)  v_\color{red}{\pi}(s&apos;) \right) 
   \\
&amp;amp;=
 \sum_{a} \color{red}{\pi}(a \mid s) \left ( \sum_{s&apos;} \sum_{r} \color{purple}{p}(s&apos;, r \mid s, a) \left[ r + \gamma v_\color{red}{\pi}(s&apos;) \right] \right) 
\tag{12}\label{12}.
\end{align*}\]

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In conclusion, value functions define the objectives of an RL problem. They can be written as recursive equations, known as &lt;em&gt;Bellman equations&lt;/em&gt;, in honor of &lt;strong&gt;Richard Bellman&lt;/strong&gt;, who made significant contributions to the theory of &lt;strong&gt;dynamic programming (DP)&lt;/strong&gt;, which is related to RL.&lt;/p&gt;

&lt;p&gt;More specifically, DP is an approach that can be used to solve MDPs (i.e. to find \(\color{green}{\pi_*}\)) when \(\color{blue}{p}\) is available, but not only &lt;sup id=&quot;fnref:9&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:9&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;8&lt;/a&gt;&lt;/sup&gt;, where the solution to a problem can be computed by combining the solution to subproblems: the Bellman equation really reflects this idea, i.e. \(q_\color{red}{\pi}(s, a)\) is computed as a function of the “subproblem” \(q_\color{red}{\pi}(s&apos;, a&apos;)\). The problem is that \(\color{blue}{p}\) is rarely available, hence the need for RL. In any case, DP algorithms, like &lt;a href=&quot;https://www.gwern.net/docs/statistics/decision/1960-howard-dynamicprogrammingmarkovprocesses.pdf&quot;&gt;policy iteration (PI)&lt;/a&gt;, and RL algorithms, like &lt;a href=&quot;http://www.cs.rhul.ac.uk/~chrisw/new_thesis.pdf&quot;&gt;Q-learning&lt;/a&gt;, are related because they assume that the environment can be modeled as an MDP and they attempt to estimate the same optimal value function.&lt;/p&gt;

&lt;!-- Although they are both approaches to solve MDPs, RL and DP are not the same thing. DP algorithms, like [policy iteration (PI)][11], need access to $$\color{blue}{p}(s&apos; \mid s, a)$$, in addition to the reward function, while RL algorithms, like [Q-learning][13], don&apos;t necessarily need to use and/or estimate $$\color{blue}{p}(s&apos; \mid s, a)$$, but they can estimate the value function by _trial-and-error_ while interacting with the environment. So, DP algorithms, like PI, are often called _planning_ (or _search_) algorithms because we don&apos;t need to interact with the environment to learn about the environment but we can simply use the dynamics to search for the optimal policy. The problem is that $$\color{blue}{p}$$ is rarely available, hence the need for RL. In any case, VI and Q-learning are related because they assume that the environment can be modeled as an MDP and they attempt to estimate the same optimal value function. --&gt;

&lt;!-- [^5]: There are also continuous-time MDPs. --&gt;
&lt;!-- [^12]: In turn, $$X=x$$ is a shorthand for $$\{\omega \in \Omega : X(\omega) = x \}$$, where $$\omega \in \Omega$$ is an outcome and $$\Omega$$ the sample space (the set of all possible outcomes), for the random variable $$X : \Omega \rightarrow E$$, where, in our case, $$E$$ is a finite set and so $$X$$ is a discrete random variable. --&gt;
&lt;!-- [^13]: The agent is also called _controller_, _player_, or _decision maker_. The environment is also called _controlled system_ or _plant_. An action is also called _control_. A policy is also called _behaviour_ or _strategy_. Finally, a reward is also called _payoff_ or _reinforcement_. --&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It’s also called &lt;em&gt;action value function&lt;/em&gt;, but I prefer to call it state-action value function because it reminds us that this is a function of a state and action. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:11&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Let \(X\) and \(Y\) be two discrete random variables and \(p(x, y)\) be their joint distribution. The &lt;a href=&quot;https://en.wikipedia.org/wiki/Marginal_distribution&quot;&gt;marginal distribution&lt;/a&gt; of \(X\) or \(Y\) can be found as \(p(x) = \sum_y p(x, y)\) and \(p(y) = \sum_x p(x, y)\), respectively. &lt;a href=&quot;#fnref:11&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:8&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The subscript \(t\) in the object \(x_t\) is used to emphasize that \(x\) is associated with the time step \(t\). &lt;a href=&quot;#fnref:8&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:4&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Hence the name &lt;em&gt;value function&lt;/em&gt;. &lt;a href=&quot;#fnref:4&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:7&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;If the reward is deterministic, then \(p(r \mid s, a)\) gives probability \(1\) to one reward and \(0\) to all other rewards. &lt;a href=&quot;#fnref:7&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:6&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;\(\pi(a \mid s)\) can also be used to describe deterministic policies by giving a probability of \(1\) to one action in \(s\) and a probability of \(0\) to all other actions. A deterministic policy might also be defined as a function \(\pi : \mathcal{S} \rightarrow \mathcal{A}\), so \(\pi(s) = a\) is the (only) action taken by \(\pi\) in \(s\). &lt;a href=&quot;#fnref:6&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:10&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;A policy \(\color{red}{\pi}(a \mid s)\) is &lt;a href=&quot;https://en.wikipedia.org/wiki/Stationary_process&quot;&gt;stationary&lt;/a&gt; if it doesn’t change over time steps, i.e. \(\color{red}{\pi}(a \mid S_{t} = s) = \color{red}{\pi}(a \mid S_{t+1} = s), \forall t, \forall s \in \mathcal{S}\), in other words, the probabilities of selecting an action do not change from time step to time step. You can think of a non-stationary policy as a set of policies. &lt;a href=&quot;#fnref:10&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:9&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;For more info about the dynamic programming approach, I recommend that you read the corresponding chapter in the book &lt;a href=&quot;https://edutechlearners.com/download/Introduction_to_algorithms-3rd%20Edition.pdf&quot;&gt;Introduction to Algorithms&lt;/a&gt; (3rd edition) by Thomas H. Cormen et al. &lt;a href=&quot;#fnref:9&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Wed, 16 Feb 2022 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2022/02/16/bellman-equations/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2022/02/16/bellman-equations/</guid>
      </item>
    
      <item>
        <title>MDPs are POMDPs</title>
        <description>&lt;p&gt;A (fully observable) &lt;strong&gt;Markov Decision Process (MDP)&lt;/strong&gt; is just a &lt;strong&gt;Partially Observable Markov Decision Process (POMDP)&lt;/strong&gt; where the states are observable. So, we can formulate an MDP as a POMDP such that the observation space is equal to the state space. We also need to take care of the observation function. Let’s see how exactly.&lt;/p&gt;

&lt;p&gt;Formally, an MDP can be defined as a tuple \(M_\text{MDP} = (\mathcal{S}, \mathcal{A}, T, r, \gamma)\), where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(\mathcal{S}\) is the state space&lt;/li&gt;
  &lt;li&gt;\(\mathcal{A}\) is the action space&lt;/li&gt;
  &lt;li&gt;\(T = p(s&apos; \mid s, a)\) is the transition function&lt;/li&gt;
  &lt;li&gt;\(r\) is the reward function&lt;/li&gt;
  &lt;li&gt;\(\gamma\) is the discount factor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A POMDP is defined as a tuple \(M_\text{POMDP} = (\mathcal{S}, \mathcal{A}, T, r, \gamma, \color{red}{\Omega}, \color{red}{O})\), where \(\mathcal{S}\), \(\mathcal{A}\), \(T\), \(r\) and \(\gamma\) are defined as above, but, in addition to those, we also have&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(\color{red}{\Omega}\): the observation space&lt;/li&gt;
  &lt;li&gt;\(\color{red}{O} = p(o \mid s&apos;, a)\): the observation function, which is the probability distribution over possible observations, given the next state \(s&apos;\) and action \(a\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, to define \(M_\text{MDP}\) as \(M_\text{POMDP}\), we have&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
\[\color{red}{\Omega} = \mathcal{S}\]
  &lt;/li&gt;
  &lt;li&gt;The observation function is
\(\color{red}{O} 
= 
p(o \mid s&apos;, a) =
\begin{cases}
1, \text{ if } o = s&apos; \\
0, \text{ otherwise }
\end{cases}\)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, the probability of observing \(o = s&apos;\), given that we end up in \(s&apos;\), is \(1\), while the probability of observing \(o \neq s&apos;\) is \(0\). This has implications on how you update the belief state \(b(s&apos;)\) because \(b(s&apos;)\) will be set to \(0\) if \(o \neq s&apos;\).&lt;/p&gt;
</description>
        <pubDate>Sat, 01 Jan 2022 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2022/01/01/mdps-are-pomdps/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2022/01/01/mdps-are-pomdps/</guid>
      </item>
    
      <item>
        <title>Historically relevant programs developed in LISP</title>
        <description>&lt;p&gt;&lt;strong&gt;LISP&lt;/strong&gt; stands for &lt;strong&gt;Lis&lt;/strong&gt;t &lt;strong&gt;P&lt;/strong&gt;rocessing. In this functional programming language, programs look like lists and can be treated as data (hence the name) &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. It was designed by John McCarthy (&lt;a href=&quot;http://jmc.stanford.edu/articles/dartmouth/dartmouth.pdf&quot;&gt;one of the official founders of the AI field&lt;/a&gt;) starting in 1958.&lt;/p&gt;

&lt;p&gt;Many people know that LISP is historically a very important programming language in Artificial Intelligence. Even today, dialects of LISP are still being used in this context. For example, &lt;a href=&quot;https://github.com/lspector/Clojush&quot;&gt;Clojush&lt;/a&gt; is a &lt;a href=&quot;https://clojure.org/&quot;&gt;Clojure&lt;/a&gt; (which is a dialect of LISP) implementation of the &lt;a href=&quot;https://faculty.hampshire.edu/lspector/push.html&quot;&gt;Push&lt;/a&gt; programming language and the &lt;a href=&quot;https://faculty.hampshire.edu/lspector/push.html&quot;&gt;PushGP&lt;/a&gt; system, which are still being used to do research on &lt;a href=&quot;http://www0.cs.ucl.ac.uk/staff/W.Langdon/ftp/papers/poli08_fieldguide.pdf&quot;&gt;genetic programming&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Many historically relevant programs were implemented in LISP in the early days of AI. Here’s a non-exhaustive &lt;strong&gt;lis&lt;/strong&gt;t &lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; &lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;
      &lt;th&gt;Author&lt;/th&gt;
      &lt;th&gt;Source&lt;/th&gt;
      &lt;th&gt;Year&lt;/th&gt;
      &lt;th&gt;Brief description/comment&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Symbolic Automatic INTegrator (SAINT)&lt;/td&gt;
      &lt;td&gt;James R. Slagle&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/321186.321193&quot;&gt;[1]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1963&lt;/td&gt;
      &lt;td&gt;A symbolic integretation program&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;ANALOGY&lt;/td&gt;
      &lt;td&gt;Thomas G. Evans&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/1464122.1464156&quot;&gt;[2]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1964&lt;/td&gt;
      &lt;td&gt;It solves geometric analogy problems&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Semantic Information Retrieval (SIR)&lt;/td&gt;
      &lt;td&gt;Bertram Raphael&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://ai.stanford.edu/~nilsson/QAI/qai.pdf#page=135&quot;&gt;[3]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1964&lt;/td&gt;
      &lt;td&gt;A “machine understanding” program&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;QA3&lt;/td&gt;
      &lt;td&gt;C. Cordell Green (and Robert Yates)&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;http://www.ai.sri.com/pubs/files/tn004-green69.pdf&quot;&gt;[4]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1969&lt;/td&gt;
      &lt;td&gt;A resolution-based deduction system, which was an attempt to improve on Raphael’s SIR; QA3 is the successor of QA2 and QA1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SEE&lt;/td&gt;
      &lt;td&gt;Adolfo Guzman-Arenas&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/1476589.1476631&quot;&gt;[5]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1969&lt;/td&gt;
      &lt;td&gt;A program to segment a line drawing of a scene containing blocks into its constituents&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;DENDRAL&lt;/td&gt;
      &lt;td&gt;Edward Feigenbaum, Joshua Lederberg, Bruce Buchanan, Carl Djerassi, and others&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://ai.stanford.edu/~nilsson/QAI/qai.pdf#page=255&quot;&gt;[6]&lt;/a&gt;, &lt;a href=&quot;https://dl.acm.org/doi/10.1145/41526.41528&quot;&gt;[7]&lt;/a&gt;, &lt;a href=&quot;https://stacks.stanford.edu/file/druid:pq644jd0400/pq644jd0400.pdf&quot;&gt;[8]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1965-&lt;/td&gt;
      &lt;td&gt;A project, expert system or series of programs to help chemists identify the structure of molecules given their mass spectra and other expert knowledge&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Stanford Research Institute Problem Solver (STRIPS)&lt;/td&gt;
      &lt;td&gt;Richard Fikes &amp;amp; Nils Nilsson&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://ai.stanford.edu/users/nilsson/OnlinePubs-Nils/PublishedPapers/strips.pdf&quot;&gt;[9]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;~1970&lt;/td&gt;
      &lt;td&gt;A planning system used in &lt;a href=&quot;https://www.youtube.com/watch?v=7bsEN8mwUB8&quot;&gt;the Shakey robot&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;SHRDLU&lt;/td&gt;
      &lt;td&gt;Terry Winograd&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;http://hci.stanford.edu/~winograd/shrdlu/AITR-235.pdf&quot;&gt;[10]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1971&lt;/td&gt;
      &lt;td&gt;A NLP dialog system, which was only partially written in LISP&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;MYCIN&lt;/td&gt;
      &lt;td&gt;Edward (Ted) Shortliffe&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;https://ai.stanford.edu/~nilsson/QAI/qai.pdf#page=291&quot;&gt;[11]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;~1970&lt;/td&gt;
      &lt;td&gt;An expert system that would consult with physicians about bacterial infections and therapy; MYCYN is a common suffix for antibacterial &lt;a href=&quot;https://hearinglosshelp.com/blog/the-ototoxicity-of-drugs-ending-in-mycin-and-micin/&quot;&gt;[12]&lt;/a&gt;; the specific version of LISP used was &lt;a href=&quot;http://www.softwarepreservation.org/projects/LISP/bbnlisp&quot;&gt;BBN-LISP&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Language Interface Facility with Elliptical and Recursive Features (LIFER)&lt;/td&gt;
      &lt;td&gt;Gary Hendrix&lt;/td&gt;
      &lt;td&gt;&lt;a href=&quot;http://www.ai.sri.com/pubs/files/1414.pdf&quot;&gt;[13]&lt;/a&gt;&lt;/td&gt;
      &lt;td&gt;1976&lt;/td&gt;
      &lt;td&gt;A program to interact with databases in a subset of natural language (e.g. English); the specific version of LISP used was &lt;a href=&quot;https://interlisp.org/&quot;&gt;INTERLISP&lt;/a&gt;, a successor of &lt;a href=&quot;http://www.softwarepreservation.org/projects/LISP/bbnlisp&quot;&gt;BBN-LISP&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;In addition to these programs, many of the implementations of the &lt;em&gt;conceptual structures&lt;/em&gt; by Roger C. Schank were in LISP &lt;a href=&quot;https://ai.stanford.edu/~nilsson/QAI/qai.pdf#page=207&quot;&gt;[8]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Later, LISP was also &lt;a href=&quot;https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/areas/genetic/gp/systems/koza/0.html&quot;&gt;used by John Koza in the context of GP&lt;/a&gt; (but this was already in the 90s). In 1998, NASA also developed in &lt;a href=&quot;http://www.lispworks.com/&quot;&gt;LISP Works&lt;/a&gt; the “Remote Agent” (RA), a robotic system for planning and executing spacecraft actions, in the context of &lt;a href=&quot;https://www.jpl.nasa.gov/missions/deep-space-1-ds1&quot;&gt;Deep Space 1&lt;/a&gt; &lt;a href=&quot;https://ai.stanford.edu/~nilsson/QAI/qai.pdf#page=603&quot;&gt;[8]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you are aware of any LISP program developed in the early days of AI (50s-90s) that is not mentioned above, you can share it with us in the comment section below and I will include it in the table above.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;I am not a LISP programmer, but 3-4 years ago I had implemented a simple plugin for Emacs in Emacs Lisp. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Most of these programs are mentioned in the book &lt;a href=&quot;https://ai.stanford.edu/~nilsson/QAI/qai.pdf&quot;&gt;The Quest for Artificial Intelligence: A History of Ideas and Achievements&lt;/a&gt;, (2009) by Nils J. Nilsson, which I’ve been reading and enjoying. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Not all of these programs were fully implemented in LISP, and it’s possible that there also other implementations of these programs in other programming languages. &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sat, 04 Dec 2021 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2021/12/04/historically-relevant-programs-developed-in-lisp/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2021/12/04/historically-relevant-programs-developed-in-lisp/</guid>
      </item>
    
      <item>
        <title>Optimal value function of shifted rewards</title>
        <description>&lt;h2 id=&quot;theorem&quot;&gt;Theorem&lt;/h2&gt;

&lt;p&gt;Consider the following &lt;strong&gt;Bellman optimality equation (BOE)&lt;/strong&gt; (&lt;a href=&quot;http://incompleteideas.net/book/RLbook2020.pdf#page=86&quot;&gt;equation 3.20 of Sutton &amp;amp; Barto book on RL, 2nd edition, p. 64&lt;/a&gt;)&lt;/p&gt;

\[q_*(s,a)
=\sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} p(s&apos;,r \mid s,a) \left(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right)\tag{1}\label{1}.\]

&lt;p&gt;If we add the same constant \(c \in \mathbb{R}\) to all rewards \(r \in \mathcal{R}\), then the new optimal state-action value function is given by&lt;/p&gt;

\[q_*(s, a) + k,\]

&lt;p&gt;where&lt;/p&gt;

\[k  = \frac{c}{1 - \gamma}
= c\left(\frac{1}{1 - \gamma}\right)
= c \left( \sum_{i=0}^{\infty} \gamma^{i} \right)
= c \left( 1 + \gamma + \gamma^2 + \gamma^3 + \dots \right),\]

&lt;p&gt;where \(0 \leq \gamma &amp;lt; 1\) is the discount factor of the MDP and \(\sum_{i=0}^{\infty} \gamma^{i}\) is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Geometric_series&quot;&gt;geometric series&lt;/a&gt; &lt;sup id=&quot;fnref:4&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:4&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;h3 id=&quot;assumptions&quot;&gt;Assumptions&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;\(0 \leq \gamma &amp;lt; 1\); if we allowed \(\gamma = 1\), then \(\frac{c}{1 - \gamma} = c/0\), which is undefined.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;For &lt;strong&gt;episodic problems&lt;/strong&gt; &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;, we &lt;strong&gt;assume&lt;/strong&gt; that we have an &lt;strong&gt;absorbing state&lt;/strong&gt; \(s_\text{absorbing}\) &lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;, which is the state that the agent moves to after it has reached the goal, where the agent gets a reward of \(0\) for all future time steps. So, \(q_*(s_\text{absorbing}, a) =0, \forall a \in\mathcal{A}(s_\text{absorbing})\).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;proof&quot;&gt;Proof&lt;/h2&gt;

&lt;p&gt;To show this, we need to show that the following equation is equivalent to the BOE in \ref{1}.&lt;/p&gt;

\[q_*(s,a) + k
= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)\left((r + c) + \gamma \max_{a&apos; \in\mathcal{A}(s&apos;)} \left( q_*(s&apos;,a&apos;) + k  \right) \right) \tag{2}\label{2}\]

&lt;p&gt;Given that \(k = \frac{c}{1 - \gamma}\) is a constant, it does not affect the max, because we add this constant to all state-action values: this holds even if \(c\) is negative! So, we can take \(k\) out of the max and add it to \(\max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;)\)&lt;/p&gt;

\[\begin{align*}
q_*(s,a) + k
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)\left((r + c) + \gamma \left (k +  \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right) \right) 
\\
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)\left((r + c) +  \frac{c \gamma}{1 - \gamma} +  \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right) 
\\
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)\left(r + \frac{c(1 - \gamma) + c \gamma}{1 - \gamma} +  \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right) 
\\
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)\left(r + \frac{c - c\gamma + c \gamma}{1 - \gamma} +  \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right) 
\\
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} \left ( p(s&apos;,r \mid s,a)\frac{c}{1 - \gamma} \right) + 
\\
&amp;amp;
\sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}  \left( p(s&apos;,r \mid s,a) \left(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right) \right).
\tag{3}\label{3}
\end{align*}\]

&lt;p&gt;Given that \(p(s&apos;,r \mid s,a)\) is a probability distribution, then \(\sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} \left ( p(s&apos;,r \mid s,a)\frac{c}{1 - \gamma} \right)\) is the expectation of the constant \(\frac{c}{1 - \gamma}\), which is equal to the constant itself.&lt;/p&gt;

&lt;p&gt;So, equation \ref{3} becomes&lt;/p&gt;

\[q_*(s,a) + \frac{c}{1 - \gamma} 
= \frac{c}{1 - \gamma}  + \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} p(s&apos;,r \mid s,a) \left(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right) \\ 
\iff \\
q_*(s,a)
=\sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} p(s&apos;,r \mid s,a) \left(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;) \right)\]

&lt;p&gt;which is the Bellman optimality equation \ref{1}.&lt;/p&gt;

&lt;h2 id=&quot;interpretation&quot;&gt;Interpretation&lt;/h2&gt;

&lt;p&gt;The result above suggests that, &lt;strong&gt;if we add a constant to all rewards&lt;/strong&gt;, which is a form of &lt;strong&gt;reward shaping&lt;/strong&gt;, &lt;strong&gt;the set of optimal policies does not change&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Is this always true? Yes, &lt;strong&gt;in theory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, we must be careful with &lt;em&gt;episodic problems&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;In theory, after we shift the rewards by \(c\), the agent will precisely get an additional reward of \(k = \frac{c}{1 - \gamma}\) for being in any state, including the &lt;em&gt;absorbing state&lt;/em&gt;, and taking any action. So, after we shift the rewards, we have \(q_*(s_\text{absorbing}, a) = \frac{c}{1 - \gamma}, \forall a \in\mathcal{A}(s_\text{absorbing})\).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;In practice&lt;/strong&gt;, one might mis-specify the reward functions, if we shift the rewards and terminate the episode once the agent gets to \(s_\text{absorbing}\).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example&quot;&gt;Example&lt;/h3&gt;

&lt;p&gt;To illustrate this issue, let’s say that, for an episodic problem (for example, a problem where the agent is in a grid and needs to go to a goal location), we have the following (deterministic) reward function&lt;/p&gt;

\[r(s, a) = 
\begin{cases}
1, \text{if } s = s_\text{goal}\\
0, \text{if } s = s_\text{absorbing}\\
0, \text{otherwise} \\
\end{cases}\]

&lt;p&gt;\(s_\text{absorbing}\) is just the state that we &lt;strong&gt;assume&lt;/strong&gt; the agent moves to after having reached the goal state, so that he continues to get a reward of \(0\), which is an &lt;strong&gt;assumption&lt;/strong&gt; that we make that allows us to terminate the episode once we get to \(s_\text{goal}\).&lt;/p&gt;

&lt;p&gt;Now, let’s say that we define a new reward function as \(r&apos;(s, a) \triangleq r(s, a) - 1\), i.e.&lt;/p&gt;

\[r&apos;(s, a) = 
\begin{cases}
0, \text{if } s = s_\text{goal}\\
-1, \text{if } s = s_\text{absorbing}\\
-1, \text{otherwise} \\
\end{cases}\]

&lt;p&gt;So, in theory, you don’t get a reward of \(0\) anymore after the agent gets to the goal with \(r&apos;\). If you terminate the episode once the agent gets to the goal, this will not be taken into account, i.e. if you terminate the episode once the agent got to the goal, then you assume that \(r&apos;(s_\text{absorbing}, a) = 0, \forall a \in \mathcal{A}(s_\text{absorbing})\), i.e. you’re actually optimizing&lt;/p&gt;

\[r&apos;&apos;(s, a) = 
\begin{cases}
0, \text{if } s = s_\text{goal}\\
0, \text{if } s = s_\text{absorbing}\\
-1, \text{otherwise} \\
\end{cases}\]

&lt;p&gt;So, in practice, you might be optimizing a different objective function than the one you &lt;em&gt;implicitly&lt;/em&gt; or &lt;em&gt;unconsciously&lt;/em&gt; assumed. In this example, \(r&apos;&apos;(s, a)\) is the reward function that encourages the agent to get to the goal as quickly as possible (because you get a penalty of \(-1\) for every time step that you have not reached the goal), so, in practice, \(r&apos;&apos;(s, a)\) might be what you want to optimize, but, in general, you must be careful with &lt;strong&gt;reward misspecification&lt;/strong&gt; &lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;!&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:4&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;\(k\) is also written as a geometric series to emphasize that \(k\) is similar to the &lt;em&gt;discounted return&lt;/em&gt;, which is defined as \(G_t = \sum_{i=0}^{\infty} \gamma^{i}R_{t+1+i}\), where \(R_{t+1+i}\) is the reward at time step \(t+1+i\). If all rewards were equal to \(c\), then \(G_t = \frac{c}{1 - \gamma}\). &lt;a href=&quot;#fnref:4&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;See &lt;a href=&quot;http://incompleteideas.net/book/RLbook2020.pdf#page=76&quot;&gt;sections 3.3 and 3.4. (p. 54)&lt;/a&gt; of Sutton &amp;amp; Barto book (2nd edition) for more details about the difference between episodic and continuing problems and how they can be unified. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The assumption of having an absorbing state is also made in &lt;a href=&quot;http://luthuli.cs.uiuc.edu/~daf/courses/games/AIpapers/ml99-shaping.pdf&quot;&gt;Policy invariance under reward transformations: Theory and application to reward shaping&lt;/a&gt; (1999) by Andrew Y. Ng et al., which is a seminal paper on reward shaping, which cites the book &lt;a href=&quot;https://jmvidal.cse.sc.edu/library/neumann44a.pdf&quot;&gt;Theory of Games and Economic Behavior&lt;/a&gt; (1944) by John von Neumann et al. to support the claim that, for single-step decisions (which I assume to be some kind of bandit problem), positive linear transformations of the utility function do not change the optimal decision/policy: if we combine the theorem in this blog post and &lt;a href=&quot;https://nbro.gitlab.io/blogging/2019/09/15/optimal-value-function-of-scaled-rewards/&quot;&gt;the theorem in my previous blog post&lt;/a&gt;, we get a similar result. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;This idea of &lt;em&gt;reward misspecification&lt;/em&gt; has been studied in the literature. For example, in the paper, &lt;a href=&quot;https://papers.nips.cc/paper/2017/hash/32fdab6559cdfa4f167f8c31b9199643-Abstract.html&quot;&gt;Inverse Reward Design&lt;/a&gt; (2017) by Dylan Hadfield-Menell et al. the authors propose an approach to deal with &lt;em&gt;proxy reward functions&lt;/em&gt; (i.e. the reward functions designed by the human, which might not be the reward functions that the human intended to define). &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sun, 01 Nov 2020 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2020/11/01/optimal-value-function-of-shifted-rewards/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2020/11/01/optimal-value-function-of-shifted-rewards/</guid>
      </item>
    
      <item>
        <title>On the definition of intelligence</title>
        <description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;There are many people that claim that we still do not agree on a definition of intelligence (and thus what constitutes an artificial intelligence), with the usual argument that intelligence means something different for different people or that we still do not understand everything about (human or animal) intelligence. In fact, in the article &lt;a href=&quot;http://www-formal.stanford.edu/jmc/whatisai/&quot;&gt;What is artificial intelligence?&lt;/a&gt; (2007), John McCarthy, one of the official founders of the AI field, states&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The problem is that we cannot yet characterize in general what kinds of computational procedures &lt;strong&gt;we want to call&lt;/strong&gt; intelligent. We understand some of the mechanisms of intelligence and not others.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To understand all mechanisms of intelligence, some people, such as Jeff Hawkins, have been studying the human brain (which is the main example of a system that is associated with intelligence).&lt;/p&gt;

&lt;p&gt;We might not know &lt;strong&gt;how&lt;/strong&gt; we are intelligent (i.e. how the human brain makes us intelligent), but this does not mean that we can’t come up with a general definition of intelligence that comprises all forms of intelligence (that people could possibly refer to). In other words, you do not need to fully understand all mechanisms of intelligence in order to attempt to provide a general definition of intelligence. For example, theoretical physicists (such as Albert Einstein) do not need to understand all the details of physics in order to come up with general laws of physics that are applicable in most cases and that explain many phenomena.&lt;/p&gt;

&lt;h2 id=&quot;universal-intelligence&quot;&gt;Universal Intelligence&lt;/h2&gt;

&lt;p&gt;There has been at least one quite serious attempt to formally define &lt;em&gt;intelligence&lt;/em&gt; (and machine intelligence), so that it comprises all forms of intelligence that people could refer to.&lt;/p&gt;

&lt;p&gt;In the paper &lt;a href=&quot;https://arxiv.org/pdf/0712.3329.pdf&quot;&gt;Universal Intelligence: A Definition of Machine Intelligence&lt;/a&gt; (2007), Legg and Hutter, after having researched many previously given definitions of intelligence, informally define intelligence as follows&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Intelligence measures an agent’s ability to achieve goals in a wide range of environments&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This definition favors systems that are able to solve many tasks, which are often known as &lt;a href=&quot;http://www.scholarpedia.org/article/Artificial_General_Intelligence&quot;&gt;&lt;strong&gt;artificial general intelligences (AGIs)&lt;/strong&gt;&lt;/a&gt;, than systems that are only able to solve a specific task, sometimes known as &lt;strong&gt;narrow AIs&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;mathematical-formalization&quot;&gt;Mathematical Formalization&lt;/h3&gt;

&lt;p&gt;To understand why this is the case, let’s look at their simple mathematical formalization of this definition (&lt;a href=&quot;https://arxiv.org/pdf/0712.3329.pdf#page=20&quot;&gt;section 3.3 of the paper&lt;/a&gt;)&lt;/p&gt;

\[\Gamma(\pi) := \sum_{\mu \in E} \frac{1}{2^{K(\mu)}} V_{\mu}^{\pi}\]

&lt;p&gt;where&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;\(\Gamma(\pi)\) is the &lt;em&gt;universal intelligence&lt;/em&gt; of agent \(\pi\)&lt;/li&gt;
  &lt;li&gt;\(E\) is the space of all &lt;em&gt;computable reward summable environmental measures with respect to the reference machine \(U\)&lt;/em&gt; (roughly speaking, the space of all environments)&lt;/li&gt;
  &lt;li&gt;\(\mu\) is the environment (or task/problem)&lt;/li&gt;
  &lt;li&gt;\(V_{\mu}^{\pi}\) is the ability of the agent \(\pi\) to achieve goals in the environment \(\mu\)&lt;/li&gt;
  &lt;li&gt;\(K(\mu)\) is the Kolmogorov complexity of the environment \(\mu\)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;interpretation&quot;&gt;Interpretation&lt;/h3&gt;

&lt;p&gt;We can immediately notice that the intelligence of an agent is a weighted combination of the ability to achieve goals in the environments (which represent the tasks/problems to be solved), where each weight is inversely proportional to the complexity of the environment (i.e. the difficulty of describing/solving the corresponding task). In other words, \(\Gamma(\pi)\) is defined as an expectation of \(V_{\mu}^{\pi}\) with respect to the probability distribution \(\frac{1}{2^{K(\mu)}}\), which Legg and Hutter call the &lt;em&gt;universal distribution&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, the higher the complexity of an environment, the less the ability of the agent to achieve goals in this environment contributes to the intelligence of the agent. In other words, the ability to solve a very difficult task successfully might not be enough to have high intelligence. You can have higher intelligence by solving many but simpler problems. Of course, an intelligent agent that solves all tasks optimally would be the optimal or perfect agent. &lt;a href=&quot;https://jan.leike.name/AIXI.html&quot;&gt;AIXI&lt;/a&gt;, developed and formalized by Hutter, is actually an optimal agent (in some sense), but, unfortunately, it is incomputable (because it uses the Kolmogorov complexity)&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Consequently, according to this definition, we could say that all animals (and maybe even other biological organisms) are &lt;em&gt;more intelligent&lt;/em&gt; than, for example, &lt;a href=&quot;https://www.nature.com/articles/nature16961&quot;&gt;AlphaGo&lt;/a&gt; or &lt;a href=&quot;https://www.sciencedirect.com/science/article/pii/S0004370201001291&quot;&gt;DeepBlue&lt;/a&gt;, because all animals solve many problems, although they might not be as difficult as Go, while AlphaGo only solves Go &lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;h3 id=&quot;open-questions&quot;&gt;Open Questions&lt;/h3&gt;

&lt;p&gt;I like this definition of universal intelligence because it implies that humans (and other animals) are more (generally) intelligent than AlphaGo or any other computer program, but it raises at least 1-2 questions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;How would we measure the difficulty of a real-world environment?&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;So, in practice, can we really compare an animal with AlphaGo? Yes, we can with intelligent tests like the &lt;a href=&quot;https://academic.oup.com/mind/article/LIX/236/433/986238&quot;&gt;Turing test&lt;/a&gt;, but can we do it with \(\Gamma(\pi)\)? The answer to this question clearly depends on the answer to the question above.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;intelligence-tests&quot;&gt;Intelligence Tests&lt;/h3&gt;

&lt;p&gt;In &lt;a href=&quot;https://arxiv.org/pdf/0712.3329.pdf&quot;&gt;the paper&lt;/a&gt;, they also discuss issues like &lt;em&gt;intelligence tests&lt;/em&gt; and their relation to the definition of intelligence: that is, is an intelligence test sufficient to define intelligence, or is an intelligence test and a definition of intelligence distinct concepts?&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In my view, it is unproductive to come up with new definitions of intelligence (unless it’s more generally applicable than the universal intelligence) or to avoid choosing one definition with the excuse that we don’t know what intelligence is. I know what intelligence is. It’s measured by \(\Gamma(\pi)\). So, I don’t need to know &lt;strong&gt;how&lt;/strong&gt; we can create an agent that is (highly) intelligent before I know &lt;strong&gt;what&lt;/strong&gt; intelligence is. It’s not matter of liking or not a definition, it’s a matter of defining a set of axioms or hypotheses and deriving other properties from them or test those hypotheses, respectively.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;\(\Gamma(\pi)\) is also a function of the Kolmogorov complexity, but this is just a definition, i.e. it does not &lt;em&gt;directly&lt;/em&gt; give you the instructions to develop intelligent agents. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Note that, according to this definition, AlphaGo is still intelligent, but just not as intelligent as animals. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Wed, 20 May 2020 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2020/05/20/on-the-definition-of-intelligence/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2020/05/20/on-the-definition-of-intelligence/</guid>
      </item>
    
      <item>
        <title>Optimal value function of scaled rewards</title>
        <description>&lt;h2 id=&quot;theorem&quot;&gt;Theorem&lt;/h2&gt;

&lt;p&gt;Consider the following &lt;strong&gt;Bellman optimality equation (BOE)&lt;/strong&gt; (&lt;a href=&quot;http://incompleteideas.net/book/RLbook2020.pdf#page=86&quot;&gt;equation 3.20 of Sutton &amp;amp; Barto book on RL, 2nd edition, p. 64&lt;/a&gt;)&lt;/p&gt;

\[q_*(s,a) = \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)}q_*(s&apos;,a&apos;)) \tag{1}\label{1}.\]

&lt;p&gt;If we multiply all rewards by the same constant \(c &amp;gt; 0 \in \mathbb{R}\), then the new optimal state-action value function is given by&lt;/p&gt;

\[cq_*(s, a).\]

&lt;h2 id=&quot;proof&quot;&gt;Proof&lt;/h2&gt;

&lt;p&gt;To prove this, we need to show that the following BOE&lt;/p&gt;

\[c q_*(s,a) 
= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)(c r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} c q_*(s&apos;,a&apos;)). \tag{2}\label{2}\]

&lt;p&gt;is equivalent to the BOE in equation \ref{1}.&lt;/p&gt;

&lt;p&gt;Given that \(c &amp;gt; 0\), then&lt;/p&gt;

\[\max_{a&apos;\in\mathcal{A}(s&apos;)} c q_*(s&apos;,a&apos;) = c\max_{a&apos;\in\mathcal{A}(s&apos;)}q_*(s&apos;,a&apos;),\]

&lt;p&gt;so \(c\) can be taken out of the \(\operatorname{max}\) operator.&lt;/p&gt;

&lt;p&gt;Therefore, the equation \ref{2} becomes&lt;/p&gt;

\[\begin{align*}
c q_*(s,a) 
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}p(s&apos;,r \mid s,a)(c r + \gamma c \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;))  \\
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}}c p(s&apos;,r \mid s,a)(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;)) \\
&amp;amp;= c \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} p(s&apos;,r \mid s,a)(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;)) \\ 
&amp;amp;\iff 
\\
q_*(s,a) 
&amp;amp;= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} p(s&apos;,r \mid s,a)(r + \gamma \max_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;)),
\end{align*}
\tag{3}\label{3}\]

&lt;p&gt;which is equal to the the Bellman optimality equation in \ref{1}, which implies that, when the reward is given by \(cr\), \(c q_*(s,a)\) is the solution to the Bellman optimality equation.&lt;/p&gt;

&lt;h2 id=&quot;interpretation&quot;&gt;Interpretation&lt;/h2&gt;

&lt;p&gt;Consequently, &lt;strong&gt;whenever we multiply the reward function by some positive constant&lt;/strong&gt;, which can be viewed as a form of &lt;strong&gt;reward shaping&lt;/strong&gt; &lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, &lt;strong&gt;the set of optimal policies does not change&lt;/strong&gt; &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;h2 id=&quot;what-if-the-constant-is-zero-or-negative&quot;&gt;What if the constant is zero or negative?&lt;/h2&gt;

&lt;p&gt;For completeness, if \(c=0\), then \ref{2} becomes \(0=0\), which is true.&lt;/p&gt;

&lt;p&gt;If \(c &amp;lt; 0\), then \(\max_{a&apos;\in\mathcal{A}(s&apos;)} c q_*(s&apos;,a&apos;) = c\min_{a&apos;\in\mathcal{A}(s&apos;)}q_*(s&apos;,a&apos;)\), so equation \ref{3} becomes&lt;/p&gt;

\[q_*(s,a) 
= \sum_{s&apos; \in \mathcal{S}, r \in \mathcal{R}} p(s&apos;,r \mid s,a)(r + \gamma \min_{a&apos;\in\mathcal{A}(s&apos;)} q_*(s&apos;,a&apos;)),\]

&lt;p&gt;which is &lt;em&gt;not&lt;/em&gt; equal to the Bellman optimality equation in \ref{1}.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;A seminal paper on reward shaping is &lt;a href=&quot;http://luthuli.cs.uiuc.edu/~daf/courses/games/AIpapers/ml99-shaping.pdf&quot;&gt;Policy invariance under reward transformations: Theory and application to reward shaping&lt;/a&gt; (1999) by Andrew Y. Ng et al. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;There can be more than one optimal policy for a given optimal value function (and Markov Decision Process) because we might have \(q_*(s,a_1) = q_*(s,a_2) \geq q_*(s,a), \text{for } a_1, a_2 \in \mathcal{A}(s) \text{ and } \forall a \in \mathcal{A}(s)\). Any greedy policy with respect to the optimal value function \(q_*(s,a)\) is an optimal policy. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sun, 15 Sep 2019 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2019/09/15/optimal-value-function-of-scaled-rewards/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2019/09/15/optimal-value-function-of-scaled-rewards/</guid>
      </item>
    
      <item>
        <title>An example of how to use VisualDL with PyTorch</title>
        <description>&lt;h2 id=&quot;abstract&quot;&gt;Abstract&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;In this blog post, I will describe my journey while looking for visualization tools for PyTorch. In particular, I will briefly describe the options I tried out, and why I opted for VisualDL. Finally, and more importantly, I will show you a simple example of how to use VisualDL with PyTorch, both to visualize the parameters of the model and to read them back from the file system, in case you need them, e.g. to plot them with another tool (e.g. with Matplotlib).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Yesterday, I have been trying to find and use a visualization tool, similar to &lt;a href=&quot;https://www.tensorflow.org/guide/summaries_and_tensorboard&quot;&gt;TensorBoard&lt;/a&gt;, but for &lt;a href=&quot;https://pytorch.org/&quot;&gt;PyTorch&lt;/a&gt;. I find this type of visualization tools very useful, because they allow me to intuitively understand how the model is behaving and, in particular, how certain parameters and hyper-parameters of the model are changing, while the model is being trained and tested. So, this type of tools are especially useful while debugging our programs or if one needs to present the model to other people (e.g. teammates) while it is being trained or tested.&lt;/p&gt;

&lt;p&gt;There isn’t still a “standard” tool for visualization in PyTorch (AFAIK). However, there are several “decent” options: &lt;a href=&quot;https://github.com/pytorch/tnt&quot;&gt;TNT&lt;/a&gt;, &lt;a href=&quot;https://github.com/lanpa/tensorboardX&quot;&gt;tensorboardX&lt;/a&gt; or &lt;a href=&quot;https://github.com/PaddlePaddle/VisualDL&quot;&gt;VisualDL&lt;/a&gt;. There may be other options, but these are, apparently, the most popular (that I found), according to the stars of the corresponding Github repositories.&lt;/p&gt;

&lt;p&gt;If you perform a search on the web, you will find discussions and questions regarding visualization tools for PyTorch, for example &lt;a href=&quot;https://discuss.pytorch.org/t/any-visualization-tools-for-pytorch-to-help-people-debugging-the-network/19540&quot;&gt;this&lt;/a&gt;, &lt;a href=&quot;https://discuss.pytorch.org/t/graph-visualization/1558/5&quot;&gt;this&lt;/a&gt; and &lt;a href=&quot;https://www.quora.com/What-are-the-different-tools-to-visualize-the-training-process-in-PyTorch&quot;&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-options&quot;&gt;The options&lt;/h2&gt;

&lt;p&gt;I tried all three options I mentioned above.&lt;/p&gt;

&lt;h3 id=&quot;tnt&quot;&gt;TNT&lt;/h3&gt;

&lt;p&gt;I first tried to use &lt;a href=&quot;https://github.com/pytorch/tnt&quot;&gt;TNT&lt;/a&gt;, which can be used for logging purposes during the training and testing process of our models. TNT actually uses &lt;a href=&quot;https://github.com/facebookresearch/visdom&quot;&gt;Visdom&lt;/a&gt; (which is a quite flexible and general visualization tool created by Facebook) to display the info in the form of plots. Therefore, you also need Visdom as a dependency.&lt;/p&gt;

&lt;p&gt;More specifically, I tried to run &lt;a href=&quot;https://github.com/pytorch/tnt/blob/master/example/mnist_with_visdom.py&quot;&gt;this TNT example&lt;/a&gt;.
To successfully run the mentioned example using PyTorch 1.0.0, you need to modify a statement (which was already deprecated, but nobody cared to update the example), which causes an error. See &lt;a href=&quot;https://github.com/pytorch/tnt/issues/108&quot;&gt;this Github issue&lt;/a&gt; (on the TNT Github repo) for more info. I didn’t like much this example (and, generalizing, TNT) because the logic of the program drastically changed (with respect to a “usual” PyTorch program) only to perform the visualization of the evolution of e.g. the training loss. To me, this seemed like a sign of inflexibility. Therefore, I tried to look for other options.&lt;/p&gt;

&lt;h3 id=&quot;tensorboardx&quot;&gt;tensorboardX&lt;/h3&gt;

&lt;p&gt;I then tried to use &lt;a href=&quot;https://github.com/lanpa/tensorboardX&quot;&gt;tensorboardX&lt;/a&gt;, which is, right now, among the three options, the one with the highest popularity (in terms of Github stars).&lt;/p&gt;

&lt;p&gt;There are several examples which show how to use this tool. You can find them &lt;a href=&quot;https://github.com/lanpa/tensorboardX/tree/master/examples&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;https://github.com/lanpa/tensorboard-pytorch-examples&quot;&gt;here&lt;/a&gt;. In particular, I tried &lt;a href=&quot;https://github.com/lanpa/tensorboard-pytorch-examples/tree/master/mnist&quot;&gt;this example&lt;/a&gt;. To use &lt;a href=&quot;https://github.com/lanpa/tensorboardX&quot;&gt;tensorboardX&lt;/a&gt;, we actually need to install &lt;a href=&quot;https://www.tensorflow.org/guide/summaries_and_tensorboard&quot;&gt;TensorBoard&lt;/a&gt; and &lt;a href=&quot;https://www.tensorflow.org/&quot;&gt;TensorFlow&lt;/a&gt;. Apart from the fact that these are not lightweight dependencies, this was also my first time using PyTorch, and the thought of needing to use TensorFlow (which I previously used in other projects) to achieve something in PyTorch made me think that I’d better just stick with TensorFlow and give up on PyTorch. Of course, this doesn’t make much sense, but I just didn’t feel this was the right direction. Therefore, I looked for another option.&lt;/p&gt;

&lt;h3 id=&quot;visualdl&quot;&gt;VisualDL&lt;/h3&gt;

&lt;p&gt;Finally, I tried &lt;a href=&quot;https://github.com/PaddlePaddle/VisualDL&quot;&gt;VisualDL&lt;/a&gt; (Visual Deep Learning), which is essentially a visualization tool very similar to TensorBoard, whose backend is written in C++, but with both a C++ and Python APIs. Its frontend or web interface is written in &lt;a href=&quot;https://vuejs.org/&quot;&gt;Vue&lt;/a&gt;. You can find its documentation at &lt;a href=&quot;http://visualdl.paddlepaddle.org/&quot;&gt;http://visualdl.paddlepaddle.org/&lt;/a&gt;. Nowadays, most machine learning frameworks and libraries are written in C++ and have a Python API, so, of course, this characteristic of VisualDL seems consistent with many other machine learning tools. One of the goals of this visualization tool is to be “cross-framework”, i.e. not to be tailored to a specific framework (like TensorFlow or PyTorch). I like flexibility, therefore this feature immediately biased me towards VisualDL. In the &lt;a href=&quot;http://visualdl.paddlepaddle.org/&quot;&gt;official website&lt;/a&gt; of the tool, it is claimed that VisualDL works with &lt;a href=&quot;https://caffe2.ai/&quot;&gt;Caffe2&lt;/a&gt;, &lt;a href=&quot;http://www.paddlepaddle.org/&quot;&gt;PaddlePaddle&lt;/a&gt;, &lt;a href=&quot;https://pytorch.org/&quot;&gt;PyTorch&lt;/a&gt;, &lt;a href=&quot;https://keras.io/&quot;&gt;Keras&lt;/a&gt; and &lt;a href=&quot;https://mxnet.apache.org/&quot;&gt;MXNet&lt;/a&gt;. I imagine that in the future more frameworks will be supported.&lt;/p&gt;

&lt;p&gt;I first read &lt;a href=&quot;https://github.com/PaddlePaddle/VisualDL&quot;&gt;the README file&lt;/a&gt; of the Github repo of VisualDL, &lt;a href=&quot;http://visualdl.paddlepaddle.org/documentation/visualdl/en/develop/getting_started/introduction_en.html&quot;&gt;the documentation&lt;/a&gt;, &lt;a href=&quot;http://visualdl.paddlepaddle.org/documentation/visualdl/en/develop/getting_started/demo/pytorch/TUTORIAL_EN.html&quot;&gt;this tutorial&lt;/a&gt; and &lt;a href=&quot;https://github.com/PaddlePaddle/VisualDL/blob/develop/demo/pytorch/pytorch_cifar10.py&quot;&gt;this example&lt;/a&gt;. There are other examples (for other frameworks) you can find &lt;a href=&quot;https://github.com/PaddlePaddle/VisualDL/tree/develop/demo&quot;&gt;here&lt;/a&gt;. VisualDL seems to be in its preliminary phases, but you can already accomplish several things that you expect to accomplish with e.g. TensorBoard. For example, you can visualize (more or less in real-time) the evolution of scalar values of your model (e.g. the learning rate or the training loss), you can also plot histograms and visualize the computational graph.&lt;/p&gt;

&lt;p&gt;However, this blog post is not dedicated to the explanation and presentation of all features of VisualDL, therefore I let the reader explore by him or herself the remaining features of VisualDL. In the next section, I will show you a brief example of how to use VisualDL with PyTorch and how to read the logging data, once it has been logged (and possibly visualized), using the API that VisualDL already provides.&lt;/p&gt;

&lt;h2 id=&quot;how-can-visualdl-be-used-to-visualize-statistics-of-pytorch-models&quot;&gt;How can VisualDL be used to visualize statistics of PyTorch models?&lt;/h2&gt;

&lt;p&gt;Before proceeding, you need to install PyTorch and VisualDL. In this example, where the source code can be found at &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example&quot;&gt;https://github.com/nbro/visualdl_with_pytorch_example&lt;/a&gt;, I installed PyTorch and VisualDL in a &lt;a href=&quot;https://www.anaconda.com/&quot;&gt;Anaconda&lt;/a&gt; environment, but you can install them as you please. If you want to exactly follow along with my example, please, read the instructions &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example&quot;&gt;here&lt;/a&gt; on how to set up your environment and run the example.&lt;/p&gt;

&lt;p&gt;I will not describe all the details inside this example, but only the ones associated with the usage of VisualDL with PyTorch.&lt;/p&gt;

&lt;p&gt;In the file &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write_visualdl_data.py&lt;/code&gt;&lt;/a&gt;, to use VisualDL to visualize the evolution of some of the metrics or statistics (specifically, the training loss, the test loss and the test accuracy) of the associated model (a CNN trained and tested on the &lt;a href=&quot;http://yann.lecun.com/exdb/mnist/&quot;&gt;MNIST dataset&lt;/a&gt;), I first imported the class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LogWriter&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visualdl&lt;/code&gt; (line &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L11&quot;&gt;11&lt;/a&gt;):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;visualdl&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LogWriter&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I then created a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LogWriter&lt;/code&gt; object (at line &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L13&quot;&gt;13&lt;/a&gt;)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;log_writer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LogWriter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;./log&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sync_cycle&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;./log&quot;&lt;/code&gt; is the name of the folder where the logging files will be placed and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sync_cycle&lt;/code&gt; is a parameter which controls when VisualDL will force the logging to be written to the file system. Have a look at &lt;a href=&quot;http://visualdl.paddlepaddle.org/documentation/visualdl/en/develop/api/initialize_logger.html#visualdl.LogWriter&quot;&gt;the documentation&lt;/a&gt; for more info.&lt;/p&gt;

&lt;p&gt;Then, at line &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L158&quot;&gt;158&lt;/a&gt; and &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L161&quot;&gt;161&lt;/a&gt;, I defined the specific loggers (which are of type “scalars”, given that the training loss, the test loss and the test accuracy are scalar values) which will be used to record statistics during respectively the training and testing phases:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;train_losses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;scalars/train_loss&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log_writer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;test_losses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;scalars/test_loss&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;test_accuracies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scalar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;scalars/test_accuracy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What this piece of code tell us is that under the &lt;a href=&quot;http://visualdl.paddlepaddle.org/documentation/visualdl/en/develop/api/initialize_logger.html#visualdl.LogWriter.as_mode&quot;&gt;“mode”&lt;/a&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;train&quot;&lt;/code&gt;, we are defining the &lt;a href=&quot;http://visualdl.paddlepaddle.org/documentation/visualdl/en/develop/api/initialize_logger.html#visualdl.LogWriter.scalar&quot;&gt;scalar&lt;/a&gt; logger &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train_losses&lt;/code&gt; which is associated with the “tag” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;scalars/train_loss&quot;&lt;/code&gt;. Similarly for the loggers associated with the mode &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;test&quot;&lt;/code&gt;. VisualDL is actually aware of these modes: they will then be useful to retrieve the logging data from the file system (we will see this in the next example).&lt;/p&gt;

&lt;p&gt;The specific scalar loggers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train_losses&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_losses&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_accuracies&lt;/code&gt; are then passed to the functions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test&lt;/code&gt; at lines &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L168&quot;&gt;168&lt;/a&gt; and &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L169&quot;&gt;169&lt;/a&gt;. The functions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test&lt;/code&gt; are called at every epoch (inside a &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L165&quot;&gt;loop&lt;/a&gt;). Inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; function, at line &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L61&quot;&gt;61&lt;/a&gt;, we add a “record” to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train_losses&lt;/code&gt; logger using:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;train_losses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_record&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Similarly, inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test&lt;/code&gt;, at line &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py#L86&quot;&gt;86&lt;/a&gt; and 87, we add respectively records for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_losses&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test_accuracies&lt;/code&gt; loggers.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;test_losses&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_record&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;test_accuracies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_record&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test_accuracy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The first argument of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_record&lt;/code&gt; method is a “tag” or “id”, which is basically a key that will be needed later to retrieve the epoch associated with the corresponding record (which, in the examples above, is either a loss or an accuracy value). I converted the record values using the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float&lt;/code&gt; to make sure they are all floating-point values.&lt;/p&gt;

&lt;p&gt;These are the only lines of code I needed to add to &lt;a href=&quot;https://github.com/pytorch/examples/blob/master/mnist/main.py&quot;&gt;the original PyTorch program&lt;/a&gt; to obtain logging and visualization functionalities using VisualDL. More specifically, I added about 10 lines, and these lines are quite self-explanatory.&lt;/p&gt;

&lt;p&gt;The following picture shows the resulting web interface of VisualDL, after having executed this example and after having waited for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; folder to be created and containing some logging files produced by VisualDL (as explained &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/README.md&quot;&gt;in this README file&lt;/a&gt;):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/visualdl.png&quot; alt=&quot;The web interface of VisualDL&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The screenshot does not completely show the bottom plot, but, of course, in the VisualDL web interface, you can scroll down. You can even expand single plots, among other things.&lt;/p&gt;

&lt;h2 id=&quot;how-can-we-read-the-logging-data-produced-by-visualdl&quot;&gt;How can we read the logging data produced by VisualDL?&lt;/h2&gt;

&lt;p&gt;During the training and testing phases of your model, VisualDL will produce some logging files, in our case, under the folder &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt;. These files are in a format which is not human-readable. They are files associated with &lt;a href=&quot;https://github.com/protocolbuffers/protobuf&quot;&gt;ProtoBuf&lt;/a&gt; (you can ignore this!).&lt;/p&gt;

&lt;p&gt;Anyway, VisualDL also allows us to read these files using its API. We may want to do this because we may need to produce Matplotlib plots using the generated data during the training and testing phases.&lt;/p&gt;

&lt;p&gt;More specifically, we can read these logging data (previously logged to a file using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LogWriter&lt;/code&gt;, as explained in the previous example) using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LogReader&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The simple Python module &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/read_visualdl_data.py&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read_visualdl_data.py&lt;/code&gt;&lt;/a&gt; does exactly this. The statements are quite self-explanatory.&lt;/p&gt;

&lt;p&gt;But, in particular, I would like to note a few things. First, at line &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/read_visualdl_data.py#L6&quot;&gt;6&lt;/a&gt;, 14, and 22, I am creating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LogReader&lt;/code&gt; but in a certain context or “mode”, and these modes correspond to the modes where the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LogWriter&lt;/code&gt;s (in the previous example) had been created (see the example above).&lt;/p&gt;

&lt;p&gt;Note that the “ids” correspond to the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;epoch&lt;/code&gt; in the previous example.&lt;/p&gt;

&lt;p&gt;Anyway, note that you should not run &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/read_visualdl_data.py&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read_visualdl_data.py&lt;/code&gt;&lt;/a&gt; before &lt;a href=&quot;https://github.com/nbro/visualdl_with_pytorch_example/blob/master/write_visualdl_data.py&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write_visualdl_data.py&lt;/code&gt;&lt;/a&gt; (or, at least, before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; folder has been created and already contains the logging files).&lt;/p&gt;

&lt;h2 id=&quot;visualdl-has-a-few-problems&quot;&gt;VisualDL has a few problems&lt;/h2&gt;

&lt;p&gt;I have chosen VisualDL (as opposed to TNT and tensorboardX), but VisualDL has a few problems too. For some reason, at least in my case, the line charts are only displayed after a few minutes: more specifically, in the example above, only towards the end of the second epoch. See e.g. &lt;a href=&quot;https://github.com/PaddlePaddle/VisualDL/issues/524&quot;&gt;this Github issue&lt;/a&gt;. Even worse, I noticed that sometimes the line charts are not displayed at all (i.e. they are blank): in that case, I need to wait for the experiment to finish or I need to restart the VisualDL server in order to see them. I have also encountered a few weird runtime error messages on the terminal (similar to the one described in &lt;a href=&quot;https://github.com/PaddlePaddle/VisualDL/issues/315&quot;&gt;this issue&lt;/a&gt;), the causes of which I don’t yet know with certainty. Furthermore, I would like to note that I only tried to visualize line charts, so it is possible that you will encounter other problems while using other features of VisualDL. Finally, I would like to note that these problems can also be due to my inexperience with VisualDL (i.e. I may have done something wrong!).&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In this blog post, I have briefly described three tools which can be used to visualize statistics of our PyTorch models, while they are being trained and tested. I particularly liked VisualDL, and so I provided two examples which show how to use VisualDL with PyTorch: one to visualize the actual statistics and the other to read them back from the file system. VisualDL is still in its infancy, but, hopefully, it will be improved and the bugs will be fixed.&lt;/p&gt;
</description>
        <pubDate>Sun, 06 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://nbro.gitlab.io//blogging/2019/01/06/an-example-of-how-to-use-visualdl-with-pytorch/</link>
        <guid isPermaLink="true">https://nbro.gitlab.io//blogging/2019/01/06/an-example-of-how-to-use-visualdl-with-pytorch/</guid>
      </item>
    
  </channel>
</rss>
